﻿using System.Xml;

namespace RDLC
{
   public enum RDLDataType
   {
      Boolean,
      DateTime,
      Integer,
      Float,
      String
   };

   public class RDLTextBox : ReportItem
   {
      //note: Action element not implemented

      public bool? CanGrow
      {
         get { return _CanGrow; }
         set
         {
            if(!_CanGrow.HasValue)
               _CanGrow = new bool();
            _CanGrow = value;
         }
      }
      private bool? _CanGrow;

      public bool? CanShrink
      {
         get { return _CanShrink; }
         set
         {
            if(!_CanShrink.HasValue)
               _CanShrink = new bool();
            _CanShrink = value;
         }
      }
      private bool? _CanShrink;

      public bool? HideDuplicates
      {
         get { return _HideDuplicates; }
         set
         {
            if(!_HideDuplicates.HasValue)
               _HideDuplicates = new bool();
            _HideDuplicates = value;
         }
      }
      private bool? _HideDuplicates;

      public string RepeatWith; // Optional
      public string ToolTip; // Optional
      //note: ToggleImage element not implemented
      private readonly string _Value;

      public RDLTextBox(string name, string value, RDLPosition position, TextboxStyle style)
         : base(name, "Textbox", position, style)
      {
         _Value = value;
      }

      protected override void WriteDetails(XmlTextWriter writer)
      {
         if (CanGrow.HasValue)
            writer.WriteElementString("CanGrow", CanGrow.Value.ToString().ToLower());

         if (CanShrink.HasValue)
            writer.WriteElementString("CanShrink", CanShrink.Value.ToString().ToLower());

         if (HideDuplicates.HasValue)
            writer.WriteElementString("HideDuplicates", CanGrow.Value.ToString());

         if(RepeatWith != null)
            writer.WriteElementString("RepeatWith", RepeatWith);

         if(ToolTip != null)
            writer.WriteElementString("ToolTip", ToolTip);

         writer.WriteElementString("Value", _Value);
      }
   }

   public class TextboxStyle : RDLStyle
   {
      public RDLColor? BackgroundColor
      {
         get { return _BackgroundColor; }
         set
         {
            if (!_BackgroundColor.HasValue)
               _BackgroundColor = new RDLColor();
            _BackgroundColor = value;
         }
      }
      private RDLColor? _BackgroundColor;
      public string BackgroundColorExpression;

      public RDLImage BackgroundImage;
      public RDLFontStyle? FontStyle
      {
         get { return _FontStyle; }
         set
         {
            if (!_FontStyle.HasValue)
               _FontStyle = new RDLFontStyle();
            _FontStyle = value;
         }
      }
      private RDLFontStyle? _FontStyle;

      public string FontFamily;

      //public RDLFontFamily? FontFamily
      //{
      //   get { return _FontFamily; }
      //   set
      //   {
      //      if (!_FontFamily.HasValue)
      //         _FontFamily = new RDLFontFamily();
      //      _FontFamily = value;
      //   }
      //}
      //private RDLFontFamily? _FontFamily;
      public uint? FontSize
      {
         get { return _FontSize; }
         set
         {
            if (IsValidFontSize(value.Value))
            {
               if(!_FontSize.HasValue)
                  _FontSize = new uint();
               _FontSize = value;
            }
         }
      }
      private uint? _FontSize;
      public RDLFontWeight? FontWeight
      {
         get { return _FontWeight; }
         set
         {
            if (!_FontWeight.HasValue)
               _FontWeight = new RDLFontWeight();
            _FontWeight = value;
         }
      }
      private RDLFontWeight? _FontWeight;
      public string Format;
      public RDLTextDecoration? TextDecoration
      {
         get { return _TextDecoration; }
         set
         {
            if (!_TextDecoration.HasValue)
               _TextDecoration = new RDLTextDecoration();
            _TextDecoration = value;
         }
      }
      private RDLTextDecoration? _TextDecoration;
      public RDLTextAlign? TextAlign
      {
         get { return _TextAlign; }
         set
         {
            if (!_TextAlign.HasValue)
               _TextAlign = new RDLTextAlign();
            _TextAlign = value;
         }
      }
      private RDLTextAlign? _TextAlign;
      public RDLVerticalAlign? VerticalAlign
      {
         get { return _VerticalAlign; }
         set
         {
            if (!_VerticalAlign.HasValue)
               _VerticalAlign = new RDLVerticalAlign();
            _VerticalAlign = value;
         }
      }
      private RDLVerticalAlign? _VerticalAlign;
      public RDLColor? Color
      {
         get { return _Color; }
         set
         {
            if (!_Color.HasValue)
               _Color = new RDLColor();
            _Color = value;
         }
      }
      private RDLColor? _Color;
      public RDLPadding Padding;
      public uint? LineHeight
      {
         get { return _LineHeight; }
         set
         {
            if (IsValidLineHeight(value))
            {
               if (!_LineHeight.HasValue)
                  _LineHeight = new uint();
               _LineHeight = value;
            }
         }
      }
      private uint? _LineHeight;
      public bool? CanSort
      {
         get { return _CanSort; }
         set
         {

            if (!_CanSort.HasValue)
               _CanSort = new bool();
            _CanSort = value;
         }
      }
      private bool? _CanSort;
      public RDLDirection? Direction
      {
         get { return _Direction; }
         set
         {
            if (!_Direction.HasValue)
               _Direction = new RDLDirection();
            _Direction = value;
         }
      }
      private RDLDirection? _Direction;

      //Todo: Language is not implemented yet     
      public RDLUnicodeBiDi? UnicodeBiDi
      {
         get { return _UnicodeBiDi; }
         set
         {
            if (!_UnicodeBiDi.HasValue)
               _UnicodeBiDi = new RDLUnicodeBiDi();
            _UnicodeBiDi = value;
         }
      }
      private RDLUnicodeBiDi? _UnicodeBiDi;
      //Todo: Calendar is not implemented yet
      //Todo: NumeralLanguage is not implemented yet
      //Todo: NumeralVarient is not implemented yet
      public RDLWritingMode? WritingMode
      {
         get { return _WritingMode; }
         set
         {
            if (!_WritingMode.HasValue)
               _WritingMode = new RDLWritingMode();
            _WritingMode = value;
         }
      }
      private RDLWritingMode? _WritingMode;

      public TextboxStyle()
      {
         
      }

      public TextboxStyle(RDLBorderColor borderColor, RDLBorderStyle borderStyle, RDLBorderWidth borderWidth) 
         : base(borderColor, borderStyle, borderWidth)
      {
      }

      protected override void WriteDetails(XmlTextWriter writer)
      {
         if (BackgroundColor.HasValue)
            writer.WriteElementString("BackgroundColor", BackgroundColor.Value.ToString());
         else if(BackgroundColorExpression != null)
            writer.WriteElementString("BackgroundColor", BackgroundColorExpression);

         if (BackgroundImage != null)
            BackgroundImage.Write(writer);

         if (FontStyle.HasValue)
            writer.WriteElementString("FontStyle", FontStyle.Value.ToString());

         if(!string.IsNullOrEmpty(FontFamily))
            writer.WriteElementString("FontFamily", FontFamily);

         //if (FontFamily.HasValue)
         //   writer.WriteElementString("FontFamily", FontFamily.Value.ToString());

         if (FontSize.HasValue)
            writer.WriteElementString("FontSize", FontSize.Value + "pt");

         if (FontWeight.HasValue)
            writer.WriteElementString("FontWeight", FontWeight.Value.ToString());

         if(Format != null) 
            writer.WriteElementString("Format", Format);

         if (TextDecoration.HasValue)
            writer.WriteElementString("TextDecoration", TextDecoration.Value.ToString());

         if (TextAlign.HasValue)
            writer.WriteElementString("TextAlign", TextAlign.Value.ToString());

         if (VerticalAlign.HasValue)
            writer.WriteElementString("VerticalAlign", VerticalAlign.Value.ToString());

         if (Color.HasValue)
            writer.WriteElementString("Color", Color.Value.ToString());

         if(Padding != null) 
            Padding.Write(writer);

         if (LineHeight.HasValue)
            writer.WriteElementString("LineHeight", LineHeight.Value.ToString());

         if (CanSort.HasValue)
            writer.WriteElementString("CanSort", CanSort.Value.ToString());

         if (Direction.HasValue)
            writer.WriteElementString("Direction", Direction.Value.ToString());

         if (UnicodeBiDi.HasValue)
            writer.WriteElementString("UnicodeBiDi", UnicodeBiDi.Value.ToString());

         if (WritingMode.HasValue)
            writer.WriteElementString("WritingMode", WritingMode.Value.ToString());
      }
   }
}
